<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title><s:if test="#session['systemName']!=null"><s:property value="#session['systemName']"/>-</s:if>用户登录</title>
	<link type="text/css" rel="stylesheet" href="<s:property value="#session.themeStyle.mainCss" default="Skin/PM/LightBlue/main.css"/>" />
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"/>
</head>
<body id="login">
	<s:form theme="simple">
	<center>
		<div id="loginBox">
			<div id="loginTitle">
				<label><s:if test="#session.systemName!=null"><s:property value="systemName"/></s:if><s:else>通用信息管理系统</s:else></label>
			</div>
			<ul>
				<li style="width: 250px; padding-top: 15px; text-align: left;">
					用户名：
					<input id="userName" type="text" value="${userName }" style="width: 163px; height: 17px; background-image: url('images/login/user.png'); background-repeat: no-repeat; padding-left: 16px;" />
				</li>
				<li style="width: 250px; padding-top: 10px; text-align: left;">
					密　码：
					<input id="password" type="password" value="${password }" style="width: 163px; height: 17px; background-image: url('images/login/suo.png'); background-repeat: no-repeat; padding-left: 16px;" />
				</li>
				<s:if test="#session.needValidateCode">
				<li style="width: 250px; padding-top: 10px; text-align: left;">
					验证码：
					<input id="veriCode" type="text" style="border: 1px solid #3497cf; width: 80px; height: 17px;">
					<img id="img" title="点击图片刷新验证码" src="images/r.png" alt="验证码" style="margin-left: 10px; cursor: pointer; vertical-align: bottom" width="80" height="22" onclick="changeValidateCode()">
				</li>
				</s:if><s:else>
				<li style="width: 250px; padding-top: 10px; text-align: left;">&nbsp;</li>
				</s:else>
				<li style="width: 250px; padding-top: 5px; text-align: left;">
					<label id="replyParamId" style="color: red; width: 310px; margin-top: 10px;">
						${message}&nbsp;
					</label>
				</li>
				<li style="width: 350px; padding-top: 5px; text-align: left; margin-left:160px; vertical-align: middle;">
					<img src="<s:property value="#session.themeStyle.path" default="Skin/PM/LightBlue"/>/images/login/dl.png" style="cursor: pointer;" onclick="validateform();" />
					<img src="<s:property value="#session.themeStyle.path" default="Skin/PM/LightBlue"/>/images/login/qx.png" style="margin-left: 25px; cursor: pointer;" onclick="reset(); " />
				</li>
			</ul>
			<div id="dingTalkLogin" style="float: left;margin-top: 40px;display: none;"><a href="javascript:ddLogin();"><img alt="钉钉扫码登录" width="64" height="64" src="images/DingTalk_logo.png" style="border-width: 0px;"><br/>钉钉扫码登录</a></div>
		</div>
	</center>
	</s:form>
	<s:hidden id="appId" name="appId"/>
	<s:hidden id="agentId" name="agentId"/>
	<s:hidden id="corpId" name="corpId"/>
	<s:hidden id="loginUrl" name="loginUrl"/>
	<s:hidden id="gotoUrl" name="gotoUrl"/>
	<script src="js/jquery.js" type="text/javascript"></script>
	<script src="js/jquery.cookie.js" type="text/javascript"></script>
	<script src="js/store.js" type="text/javascript"></script>
	<script src="js/login.js" type="text/javascript"></script>
	<script type="text/javascript">
	
		var appId;
		var agentId;
		var corpId;
		var redirect_uri;
		var gotoUrl;
		var ddLogin;

		if (window.parent.length>0){
			window.parent.location.href = window.location.href;
		}

		$(document).ready(function(){
			changeValidateCode();
			var userName = getCookie("USER_NAME");
			if(userName != null && userName != ""){
				$("#userName").val(userName);
			}
			
			$(document).keydown(function(event){
				var key_code = event.keyCode;
				if (key_code==13){
					validateform();
				}
			});
			
			$('#userName').focus();
			
			appId = $("#appId").val();
			agentId = $("#agentId").val();
			corpId = $("#corpId").val();
			redirect_uri = $("#loginUrl").val();
			gotoUrl = $("#gotoUrl").val();
			
			if(appId!=""){
				$("#dingTalkLogin").show();
				var ddJs = "https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js";
				if (typeof (corpId) != "undefined" && corpId !=null && corpId != "") {
					ddJs = "https://g.alicdn.com/dingding/dingtalk-jsapi/2.7.13/dingtalk.open.js";
					
					loadJS(ddJs, function() {
						dd.runtime.permission.requestAuthCode({
							corpId : corpId, // 企业id
							onSuccess : function(info) {
								code = info.code // 通过该免登授权码可以获取用户身份
								alert("code="+code);
							},
							onFail : function(){
								alert("onFail");
							}
						});
	
					});
				}else{
					
					var hanndleMessage = function (event) {
						var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
						var origin = event.origin;
						var baseUrl =  "https://oapi.dingtalk.com";
						var interfaceUrl = "/connect/oauth2/sns_authorize";
						var baseParams="?appid="+appId+"&response_type=code&scope=snsapi_login&state=STATE";
						var redirectUri = "&redirect_uri="+redirect_uri+"%3DloginTmpCode%26"+loginTmpCode;
						window.location.href=baseUrl+interfaceUrl+baseParams+redirectUri+"&loginTmpCode="+loginTmpCode;
					};
					
					if (typeof window.addEventListener != 'undefined') {
					    window.addEventListener('message', hanndleMessage, false);
					} else if (typeof window.attachEvent != 'undefined') {
					    window.attachEvent('onmessage', hanndleMessage);
					}
					
					ddLogin = function (){
						var obj = DDLogin({
						     id:"dingTalkLogin",//这里需要你在自己的页面定义一个HTML标签并设置id，例如<div id="login_container"></div>或<span id="login_container"></span>
						     goto: gotoUrl,
						     style: "",
						     href: "",
						     width : "300px",
						     height: "300px"
						 });
					}
					
					$("#dingTalkLogin").show();
					
					// https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js
					// https://g.alicdn.com/dingding/dingtalk-jsapi/2.7.13/dingtalk.open.js
					
					loadJS(ddJs, function(){
						
					});
				
				}

			}
		});
		
	</script>
</body>
</html>